package com.jsh.erp.utils;

import java.util.Base64;
import java.util.Base64.Decoder;

/**
 * @ClassName: Base64Decoder
 * @ClassNameExplain: base64解码工具类
 * @author zhangfangming
 * @date 2016年8月2日
 */
public class Base64Decoder {

	/**
	 * @param msgContent 需要解码字符
	 * @return
	 * @Title: getFromBASE64
	 * @TitleExplain:
	 * @Description: 将base64加密字符解码
	 * @version
	 * @author zhangfangming-mac
	 */
	public static String getFromBASE64(String msgContent) {
		if (msgContent == null) {
			return null;
		}

		Decoder decoder = Base64.getDecoder();
		try {
			byte[] b = decoder.decode(msgContent);
			return new String(b, "utf-8");
		} catch (Exception e) {
			return null;
		}

	}

	/**
	 * @param msgContent 需要加密字符
	 * @return
	 * @Title: toBase64
	 * @TitleExplain:
	 * @Description: 使用base64加密字符
	 * @version
	 * @author zhangfangming-mac
	 */
	public static String toBase64(String msgContent) {
		return Base64.getEncoder().encodeToString(msgContent.getBytes());
	}

	public static void main(String[] args) {
		String base64 = toBase64("hello你好");
		System.out.println(base64);
		System.out.println(getFromBASE64(base64));
	}

}
